package com.nhim.cms.util;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.nhim.cms.dao.ContentTypeDAO;
import com.nhim.cms.domain.ContentType;

@Service
public class ContentTypeManager {
	@Autowired
	private ContentTypeDAO contentTypeDAO;

	@PostConstruct
	public void init() throws Exception {
	}

	@PreDestroy
	public void destroy() {
	}

	@Transactional(readOnly = true)
	public ContentType findById(Integer id) {

		return contentTypeDAO.findById(id);

	}

	@Transactional(readOnly = true)
	public List<ContentType> findAll() {
		return contentTypeDAO.findAll();
	}

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void save(ContentType entity) throws Exception {
		
		contentTypeDAO.save(entity);
	}

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public Boolean delete(Integer id) throws Exception {

		ContentType contentType = contentTypeDAO.findById(id);

		if (contentType != null)
		{
			contentTypeDAO.delete(contentType);
			return true;
		}
		return false;

	}

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public static ContentType saveOrUpdate(ContentType entity)
			throws Exception {
				return null;

		
	}

	public static ContentTypeManager getFromApplicationContext(
			ApplicationContext ctx) {
		return (ContentTypeManager) ctx
				.getBean(ContentTypeManager.class);
	}

}
